package com.ccp.dev.system.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.system.dao.UserRoleDao;
import com.ccp.dev.system.model.UserRole;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户角色映射表 Service类
 * @author y
 */
@Service
public class UserRoleService extends BaseService<UserRole> {
	@Resource
	private UserRoleDao userRoleDao;


	/**
	 * 根据用户id删除关联数据
	 * @param userId 用户id
	 */
	@Transactional(rollbackFor = Exception.class)
	public void  delByUserId(String userId){
		this.userRoleDao.delByUserId(userId);
	}
	/**
	 * 根据角色id删除关联数据
	 * @param roleId 角色id
	 */
	@Transactional(rollbackFor = Exception.class)
	public void  delByRoleId(String roleId){
		this.userRoleDao.delByRoleId(roleId);
	}

	/**
	 * 给角色添加用户
	 * @param userIds  用户id
	 * @param roleId   角色id
	 */
	@Transactional(rollbackFor = Exception.class)
	public void  roleAddUser(String[] userIds,String roleId){
		//先将原有数据清空
		this.userRoleDao.delByRoleId(roleId);

		List<UserRole> userRoleList = new ArrayList<>();
		if(BeanUtils.isNotEmpty(userIds)){
			//循环添加UserRole类型集合
			for(String userId : userIds){
				UserRole userRole = new UserRole();
				userRole.setUserId(userId);
				userRole.setRoleId(roleId);
				userRole.setUserRoleId(UUIDUtils.getUUIDFor32());
				userRoleList.add(userRole);
			}
		}
		//批量添加数据
		if(!userRoleList.isEmpty()){
			this.userRoleDao.addList(userRoleList);
		}
	}
	/**
	 * 给用户添加角色
	 * @param userId  用户id
	 * @param roleIds   角色id
	 */
	@Transactional(rollbackFor = Exception.class)
	public void userAddRole(String userId,String[] roleIds){
		if(null == roleIds || roleIds.length == 0){
			return;
		}
		UserRole userRole = new UserRole();
		for(String roleId : roleIds){
			userRole.setUserRoleId(UUIDUtils.getUUIDFor32());
			userRole.setUserId(userId);
			userRole.setRoleId(roleId);
			this.userRoleDao.add(userRole);
		}
	}

	/**
	 * 给角色添加用户
	 * @param roleId 角色id
	 * @param userIds 用户id
	 */
	@Transactional(rollbackFor = Exception.class)
	public void addUser(String roleId,String[] userIds){
		roleAddUser(userIds,roleId);
	}

	/**
	 * 根据用户获取关联关系
	 * @param userId  用户id
	 * @return list
	 */
	public List<UserRole> queryByUserId(String userId){
		return this.userRoleDao.queryByUserId(userId);
	}
	/**
	 * 根据角色获取关联关系
	 * @param roleId  角色id
	 * @return list
	 */
	public List<UserRole> queryByRoleId(String roleId){
		return this.userRoleDao.queryByRoleId(roleId);
	}

	/**
	 * 根据多个角色获取用户
	 * @param roleIds
	 * @return
	 */
    public List<UserRole> queryUserByRoleIds(String roleIds) {
//		Map params=new HashMap();
//		params.put("roleIds", roleIds);
    	return this.userRoleDao.getUserByRoleIds(roleIds);
    }

	/**
	 * 根据用户id获取对应的角色。
	 *
	 * @param userId
	 * @return
	 */
	public List<UserRole> getByUserId(String userId) {
		return this.userRoleDao.getByUserId(userId);
	}
}
